<%@page import="java.awt.Color"%>
<%@page import="java.io.File"%>
<%@page import="javax.swing.ImageIcon"%>
<%@page import="sun.awt.image.codec.JPEGImageEncoderImpl"%>
<%@page import="com.sun.image.codec.jpeg.JPEGImageEncoder"%>
<%@page import="java.awt.Graphics2D"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page import="java.io.OutputStream"%>
<%@ page language="java" contentType="image/jpg" %><%
	if (request.getParameter("nom") != null) { 
		String carpeta = request.getRequestURI();
		carpeta = carpeta.substring(request.getContextPath().length());
		if (carpeta.indexOf('/') != -1) {
			carpeta = carpeta.substring(0, carpeta.lastIndexOf('/'));
		}
		String nom = request.getParameter("nom").toLowerCase();

	    int w = 0;
	    int h = 0;
	    
	    ImageIcon[] icons = new ImageIcon[nom.length()];
		for (int i = 0; i < nom.length(); i++) {
			char caracter = nom.charAt(i);
			File fitxer = null;
			if (caracter == ' ') {
				fitxer = new File(application.getRealPath(carpeta + "/imatges/blanc.gif"));
			}  else {
				fitxer = new File(application.getRealPath(carpeta + "/imatges/" + caracter + ".gif"));
				if (!fitxer.exists()) {
					fitxer = new File(application.getRealPath(carpeta + "/imatges/desconegut.gif"));
				}
				icons[i] = new ImageIcon(fitxer.getAbsolutePath());
				
				w += icons[i].getIconWidth();
				if (icons[i].getIconHeight() > h) {
					h = icons[i].getIconHeight();
				}
			}
		}
		
		OutputStream stream = response.getOutputStream();
	//	Image img = Jimi.getImage(application.getResourceAsStream("/images/76.jpg"), "image/jpg");
	    
		BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
	
		Graphics2D g = (Graphics2D)img.getGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, w, h);
		
		int posicio = 0;
		for (int i = 0; i < icons.length; i++) {
			int height = h - icons[i].getIconHeight();
			height = height / 2;
			g.drawImage(icons[i].getImage(), posicio, height, null);
			posicio += icons[i].getIconWidth();
		}
		/*
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, 80, 40);
	
		g.setColor(Color.RED);
		g.drawRect(5, 5, 70, 30);
		*/
		JPEGImageEncoder encoder = new JPEGImageEncoderImpl(stream);
		encoder.encode(img);
		//Jimi.putImage("image/jpg", img, stream);
	}
%>